//
//  GHSeleJokeVC.m
//  VideoPic
//
//  Created by GH on 16/3/25.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "GHSeleJokeVC.h"
#import <YYWebImage.h>
#import "NetworkingHandle.h"
#import "GHSeleTabCell.h"
#import "GHSeleModel.h"

@interface GHSeleJokeVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,retain)UITableView *tableview;
@property (nonatomic ,retain)UILabel *labelOfUserName;
@property (nonatomic ,retain)UILabel *labelContect;
@property (nonatomic ,retain)UIView *headView;
@property (nonatomic ,retain)UIImageView *imageOfHead;
@property (nonatomic ,retain)UIButton *buttonWithDZ;
@property (nonatomic ,retain)UIButton *buttonWithdigg;
@property (nonatomic ,retain)UIButton *buttonComm;
@property (nonatomic ,retain)UIButton *buttonShare;
@property (nonatomic ,retain)UILabel *labelWithDZ;
@property (nonatomic ,retain)UILabel *labelWithdigg;
@property (nonatomic ,retain)UILabel *labelOfComm;
@property (nonatomic ,retain)UILabel *labelOfshare;
@property (nonatomic ,retain)NSMutableArray *arrTop;
@property (nonatomic ,retain)NSMutableArray *arrRect;

@end

@implementation GHSeleJokeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self data];
    [self headViewOftableview];


    self.tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableview];
    self.tableview.tableHeaderView = self.headView;
    
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    
    [self.tableview registerClass:[GHSeleTabCell class] forCellReuseIdentifier:@"pool"];

}

- (void)headViewOftableview {
    self.headView = [[UIView alloc] init];
    //image
    self.imageOfHead = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, SCREEN_WIDTH / 10, SCREEN_WIDTH / 10)];
    [self.headView addSubview:self.imageOfHead];
    self.imageOfHead.yy_imageURL = [NSURL URLWithString:self.model.avatar_url];
    self.imageOfHead.layer.masksToBounds = YES;
    self.imageOfHead.layer.cornerRadius = SCREEN_WIDTH / 20;
    

    
    
    self.labelOfUserName = [[UILabel alloc] initWithFrame:CGRectMake(30 + SCREEN_WIDTH / 10, 20 + SCREEN_WIDTH/ 20 - 13, SCREEN_WIDTH/2, 25)];
    [self.headView addSubview:self.labelOfUserName];
    self.labelOfUserName.text = self.model.name;
    self.labelOfUserName.font = [UIFont systemFontOfSize:13];
    
    
    //contect
    self.labelContect = [[UILabel alloc] init];
    [self.headView addSubview:self.labelContect];
    self.labelContect.text = self.model.content;
    self.labelContect.frame = CGRectMake(20, 20 + SCREEN_WIDTH /10 +15, SCREEN_WIDTH  - 40, [self heightForLabel:self.labelContect.text]);
    self.labelContect.numberOfLines = 0;

    //all height
    CGFloat height = [self heightForLabel:self.model.content]  + [UIScreen mainScreen].bounds.size.width / 10 + 60 + 65;
    
    CGFloat userHeight = [self heightForLabel:self.labelContect.text] + 35 + SCREEN_WIDTH / 10 + 15;
    
    //四个功能键
    self.buttonWithDZ = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.headView addSubview:self.buttonWithDZ];
    self.buttonWithDZ.frame = CGRectMake(20, userHeight + 35, 20, 20);
    [self.buttonWithDZ setImage:[UIImage imageNamed:@"zan"] forState:UIControlStateNormal];
    
    
    //digg
    self.buttonWithdigg = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.headView addSubview:self.buttonWithdigg];
    self.buttonWithdigg.frame = CGRectMake(SCREEN_WIDTH / 3 - 10, userHeight + 35, 20, 20);
    [self.buttonWithdigg setImage:[UIImage imageNamed:@"bad"] forState:UIControlStateNormal];
    
    //comm
    self.buttonComm = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.headView addSubview:self.buttonComm];
    self.buttonComm.frame = CGRectMake(SCREEN_WIDTH /3 *2 - 40, userHeight + 35, 20, 20);
    [self.buttonComm setImage:[UIImage imageNamed:@"pinglun"] forState:UIControlStateNormal];
    
    
    //share
    self.buttonShare = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.headView addSubview:self.buttonShare];
    self.buttonShare.frame = CGRectMake(SCREEN_WIDTH - 80, userHeight +35, 20, 20);
    [self.buttonShare setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    //label
    
    self.labelWithDZ = [[UILabel alloc] init];
    [self.headView addSubview:self.labelWithDZ];
    self.labelWithDZ.frame = CGRectMake(45, userHeight + 35, 45, 20);
    self.labelWithDZ.font = [UIFont systemFontOfSize:13];
    self.labelWithDZ.textColor = [UIColor grayColor];
    
    
    self.labelWithdigg = [[UILabel alloc] init];
    [self.headView addSubview:self.labelWithdigg];
    self.labelWithdigg.frame = CGRectMake(SCREEN_WIDTH / 3- 10 + 20 + 10, userHeight + 35, 40, 20);
    self.labelWithdigg.font = [UIFont systemFontOfSize:13];
    self.labelWithdigg.textColor = [UIColor grayColor];
    
    self.labelOfComm = [[UILabel alloc] init];
    [self.headView addSubview:self.labelOfComm];
    self.labelOfComm.frame = CGRectMake(SCREEN_WIDTH /3 *2 - 40 + 30, userHeight +35, 50, 20);
    self.labelOfComm.font = [UIFont systemFontOfSize:13];
    self.labelOfComm.textColor = [UIColor grayColor];
    
    self.labelOfshare = [[UILabel alloc] init];
    [self.headView addSubview:self.labelOfshare];
    self.labelOfshare.frame = CGRectMake(SCREEN_WIDTH -50, userHeight + 35, 40, 20);
    self.labelOfshare.font = [UIFont systemFontOfSize:13];
    self.labelOfshare.textColor = [UIColor grayColor];
    self.labelWithDZ.text = self.model.digg_count.description;
    self.labelWithdigg.text =self.model.bury_count.description;
    
    
    self.labelOfComm.text = self.model.comm.description;
    
    self.labelOfshare.text = self.model.share_count.description;

    self.headView.frame = CGRectMake(0, 0, SCREEN_WIDTH, height);
    
//
}

-(CGFloat)heightForLabel:(NSString *)text{
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 40, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    
    
    return rect.size.height;
}

- (void)data {
    self.arrTop = [NSMutableArray array];
    self.arrRect = [NSMutableArray array];
//    NSLog(@"%@",self.url);
    [NetworkingHandle handleWorkData:self.url AndBody:nil value:nil handleBlock:^(id result) {

       NSDictionary *dicData = [result objectForKey:@"data"];
      NSArray *arrTop = [dicData objectForKey:@"top_comments"];
        
        NSArray *arrRecent = [dicData objectForKey:@"recent_comments"];
        for (NSDictionary *dicRec in arrRecent) {
            GHSeleModel *model = [[GHSeleModel alloc] init];

         model.descr = [dicRec objectForKey:@"description"];
            model.textcon = [dicRec objectForKey:@"text"];
            model.userName = [dicRec objectForKey:@"user_name"];
            model.userUrl = [dicRec objectForKey:@"user_profile_image_url"];
            model.createTime = [dicRec objectForKey:@"create_time"];
            [self.arrRect addObject:model];
        }
        if (arrTop.count) {
            for (NSDictionary *dicTop in arrTop) {
                GHSeleModel *model = [[GHSeleModel alloc] init];
                
                [model setValuesForKeysWithDictionary:dicTop];
//                model.text = [dicTop objectForKey:@"text"];
                [self.arrTop addObject:model];
            }
        }
        
        
        [self.tableview reloadData];
    }];


}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (section == 0) {
        
        return self.arrTop.count;
    }else {
    
        return self.arrRect.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    GHSeleTabCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    
    if (indexPath.section == 0) {
        GHSeleModel *model = [self.arrTop objectAtIndex:indexPath.row];
        [cell passWithModelForNew:model];
        
    }else if (indexPath.section == 1) {
        GHSeleModel *model = [self.arrRect objectAtIndex:indexPath.row];
        [cell passWithModel:model];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        
        GHSeleModel *model = [self.arrTop objectAtIndex:indexPath.row];
        return [GHSeleTabCell heightForcellWithTop:model];
    }else {
        GHSeleModel *model = [self.arrRect objectAtIndex:indexPath.row];
        return [GHSeleTabCell heightForcellWith:model];
    
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    [tableView addSubview:view];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc] initWithFrame:view.frame];
    label.font = [UIFont systemFontOfSize:13];
    label.textColor = [UIColor colorWithRed:0.728 green:0.616 blue:0.151 alpha:1.000];
    [view addSubview:label];
    if (section == 0) {
        label.text = @"    热门评论";
    }else {
        label.text = @"    新鲜评论";
    }

    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 50;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
